<?php


namespace app\loan\model;


use app\user\model\User as UserModel;
use think\Exception;
use think\facade\Hook;
use think\Model;

class Reference extends Model
{

    protected $autoWriteTimestamp = 'datetime';


    public static function isReference($number){
        $count = self::where('number',$number)->count();
        return $count>0?true:false;
    }


    public static function createBackendAccount($data){
        if ($user = UserModel::create($data)) {
            Hook::listen('user_add', $user);
            // 记录行为
            action_log('user_add', 'admin_user', $user['id'], UID);
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param $data
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function updateBackendAccount($data){
        if ($user =  UserModel::update($data,['username'=>$data['phone']])) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * @param $id
     * @return bool
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public static function deleteAccount($id){
            $phone = self::where('id',$id)->value('phone');
            $a     = self::where('id',$id)->delete();
            $b     = UserModel::where('username',$phone)->delete();
            if($a&&$b)  return true;
            return false;
    }

}